<?php
include_once($_SERVER['DOCUMENT_ROOT']."/../inc/bdd/inc.bdd_tools.class.php");
include_once($_SERVER['DOCUMENT_ROOT']."/../inc/tools/fwHelp.class.php");
/**
 * Class qui permet de g�rer le dictionnaire dynamique
 * @package FrameWork
 * @subpackage Tools
 * @author J�r�my DERUSSE <jderusse@jetmultimedia.fr>
 * @version 1.0 (13/03/2006)
 */
class fwDictionnary
{
	// --------------------------------
	// Public Variables
	// --------------------------------
	/** Langue du dictionnaire dans laquelle se feront les traductions
	 *  @var string */
	var $language;

	// --------------------------------
	// Public Methods
	// --------------------------------
	/**
	 * Constructeur de la classe
	 *
	 * @param string $p_language langue qui sera utilis�e pour la traduction
	 */
	function fwDictionnary($p_language="")
	{
		if($p_language=="")
		{
			if(isset($_SESSION["connected_user"]))
			{
				$this->language = $_SESSION["connected_user"]->language;
			}
			else
			{
				$this->language = DEFAULT_LANGUAGE;
			}
		}
		else
		{
			$this->language = $p_language;
		}
	}

	/**
	 * D�marre la mise en buffer qui permetra de lancer une traduction par la suite
	 *
	 */
	function start()
	{
		ob_start();
	}

	/**
	 * Arr�te le buffer et lance la traduction
	 *
	 */
	function stop()
	{
		$this->translate();
		ob_end_flush();
	}

	/**
	 * Traduit le contenu du buffer en fonction des dictionnaire et compl�te si besoins une dictionnaire de smots inconnus
	 *
	 */
	function translate()
	{
		global $bdd;

		//on recupère le buffer de sortie dans une variable temporaire
		$inner = ob_get_contents();

		//on recherche les mots a traduire;
		$out="";
		preg_match_all("`\{([a-zA-Z]+[a-zA-Z0-9\\_\\ ]+)\}`", $inner, $out, PREG_PATTERN_ORDER);

		$words = array_unique($out[1]);
		$u_word=array();
		foreach ($words as $w)
		{
			$u_word[] = strtoupper($w);
		}
		$words=$u_word;

		//si il n'y a aucun mot a traduire, on ne va pas plus loin
		if(count($words)==0)
		{
			return;
		}

		//on vide le buffer
		ob_clean();
		//on traduit les mots bufferis�s (on fait des paquets de 20 mots...)
		$stayingWords = $words;
		$translated = array();
		$cpt=0;
		while(count($stayingWords)!=0 && $cpt++<10)
		{
			$tmp_words = array_slice($stayingWords, 0, 20);
			$stayingWords = array_slice($stayingWords, 20);

			$in_words = "'" . join("','", $u_word) . "'";

			$sql = "SELECT ";
			$sql.= "	dic_s_word, dic_s_translation_".$this->language;
			$sql.= " FROM ";
			$sql.= "	dictionnary ";
			$sql.= " WHERE ";
			$sql.= "	dic_s_word IN(".$in_words.")";

			$datas = $bdd->fwBddGetData($sql);
			foreach ($datas as $row)
			{
				$inner = str_ireplace("{".$row["dic_s_word"]."}", $row["dic_s_translation_".$this->language], $inner);
				$translated[] = $row["dic_s_word"];
			}
		}
		$not_translated = array_unique(array_diff($words, $translated));

		foreach ($not_translated as $word)
		{
			$sql = "INSERT";
			$sql.= "	dictionnary";
			$sql.= " SET";
			$sql.= "	dic_s_word='".$word."'";
			$sql.= "	,dic_s_translation_fr='{".$word."}'";
			$sql.= "	,dic_s_translation_uk='{".$word."}'";
			$sql.= "	,dic_s_translation_it='{".$word."}'";
			$sql.= "	,dic_s_translation_es='{".$word."}'";
			$sql.= "	,dic_d_created=NOW()";
			$sql.= "	,dic_d_maj=NOW()";
			$bdd->fwBddPostData($sql);
		}
		//on remplit le buffer avec notre contenue traduit...
		fwHelp::Build($inner);
		echo $inner;
	}
	function getMotDictionnaire($motcle){
		global $bdd;
		$motcle = trim($motcle);

		if (substr($motcle,0,1)=='{') $motcle1 = substr(strtoupper($motcle),1,strlen($motcle)-2);
		else $motcle1=strtoupper($motcle);

		$lang = "dic_s_translation_".$this->language;
		$query = " SELECT ".$lang;
		$query.= "
								FROM dictionnary
								WHERE dic_s_word = '$motcle1'
							";

			$data = $bdd->fwBddGetData($query);

			if (is_array($data[0])){
				return $data[0][0];
			}else	return $motcle;
	}

	function getInfoWord($word){
		global $bdd;
		$query = "
							SELECT *
							FROM dictionnary
							WHERE dic_s_word='".$word."'";
		$data = $bdd->fwBddGetData($query);
		if (!empty($data[0]))	return $data[0];
		else return 0;
	}
	function insertRowDic($word){
		global $bdd;
		$word_fr='{'.$word.'}';
		$query = "
							INSERT INTO dictionnary(dic_s_word,dic_s_translation_fr,dic_s_translation_uk,dic_s_translation_it,dic_s_translation_es,dic_d_created,dic_d_maj)
							VALUES('$word','$word_fr','$word_fr','$word_fr','$word_fr',NOW(),NOW())	";

		$bdd->fwBddPostData($query);
	}
	function insertNewWord($motCle,$wordUpdate_fr,$wordUpdate_uk,$wordUpdate_it,$wordUpdate_es){
		global $bdd;
		if ($wordUpdate_fr!='') $wordUpdate_fr = str_replace("'","''",$wordUpdate_fr);
		if ($wordUpdate_uk!='') $wordUpdate_uk = str_replace("'","''",$wordUpdate_uk);
		if ($wordUpdate_it!='') $wordUpdate_it = str_replace("'","''",$wordUpdate_it);
		if ($wordUpdate_es!='') $wordUpdate_es = str_replace("'","''",$wordUpdate_es);

		$query = "
							INSERT INTO dictionnary(dic_s_word,dic_s_translation_fr,dic_s_translation_uk,dic_s_translation_it,dic_s_translation_es,dic_d_created,dic_d_maj)
							VALUES('$motCle','$wordUpdate_fr','$wordUpdate_uk','$wordUpdate_it','$wordUpdate_es',NOW(),NOW())";

		$bdd->fwBddPostData($query);
	}
	function updateWord($motCle,$motCleUpdate,$typeL){
		global $bdd;
		$query = "update dictionnary ";
		$motCleUpdate = str_replace("'","''",$motCleUpdate);
		switch ($typeL){
			case 1:
				$query.= "set dic_s_translation_fr='".$motCleUpdate."'";
				break;
			case 2:
				$query.= "set dic_s_translation_uk='".$motCleUpdate."'";
				break;
			case 3:
				$query.= "set dic_s_translation_it='".$motCleUpdate."'";
				break;
			case 4:
				$query.= "set dic_s_translation_es='".$motCleUpdate."'";
				break;
		}
		$query.= "
								,dic_d_maj=NOW()	where dic_s_word='".$motCle."'";

		$bdd->fwBddPostData($query);
	}
	function updateAllWords($motCle,$wordUpdate_fr,$wordUpdate_uk,$wordUpdate_it,$wordUpdate_es){
		global $bdd;

		if ($wordUpdate_fr=='' || $wordUpdate_fr==null) $wordUpdate_fr='{'.$motCle.'}';
		else $wordUpdate_fr = str_replace("'","''",$wordUpdate_fr);
		if ($wordUpdate_uk=='' || $wordUpdate_uk==null) $wordUpdate_uk='{'.$motCle.'}';
		else $wordUpdate_uk = str_replace("'","''",$wordUpdate_uk);
		if ($wordUpdate_it=='' || $wordUpdate_it==null) $wordUpdate_it='{'.$motCle.'}';
		else $wordUpdate_it = str_replace("'","''",$wordUpdate_it);
		if ($wordUpdate_es=='' || $wordUpdate_es==null) $wordUpdate_es='{'.$motCle.'}';
		else $wordUpdate_es = str_replace("'","''",$wordUpdate_es);
		$query = "update dictionnary ";
		$query.= "set dic_s_translation_fr='".$wordUpdate_fr."'";
		$query.= ",dic_s_translation_uk='".$wordUpdate_uk."'";
		$query.= ",dic_s_translation_it='".$wordUpdate_it."'";
		$query.= ",dic_s_translation_es='".$wordUpdate_es."'";
		$query.= " ,dic_d_maj=NOW() where dic_s_word='".$motCle."'";
		$bdd->fwBddPostData($query);
	}
	function getInfoWordLang($lang,$word,$wordBase=1){
		global $bdd;
		switch ($wordBase){
			case 1:
				$strWordBase = ", dic_s_translation_fr as motBase ";
				break;
			case 2:
				$strWordBase = ", dic_s_translation_uk as motBase ";
				break;
			case 3:
				$strWordBase = ", dic_s_translation_it as motBase ";
				break;
			case 4:
				$strWordBase = ", dic_s_translation_es as motBase ";
				break;
			default :
				$strWordBase = ", dic_s_translation_fr as motBase ";
				break;
		}
		switch ($lang){
			case 1:
				$query= "
									SELECT dic_s_word,dic_s_translation_fr as motTraduit $strWordBase
									FROM dictionnary
								";
				if ($word!='' && $word!=null) $query.=" WHERE dic_s_translation_fr like '{%".$word."%}'";
				else 	$query.= " WHERE dic_s_translation_fr like '{%}'";
				break;
			case 2:
				$query= "
									SELECT dic_s_word,dic_s_translation_uk as motTraduit $strWordBase
									FROM dictionnary
								";
				if ($word!='' && $word!=null) $query.=" WHERE dic_s_translation_uk like '{%".$word."%}'";
				else 	$query.= " WHERE dic_s_translation_uk like '{%}'";
				break;
			case 3:
				$query= "
									SELECT dic_s_word,dic_s_translation_it as motTraduit $strWordBase
									FROM dictionnary
								";
				if ($word!='' && $word!=null) $query.=" WHERE dic_s_translation_it like '{%".$word."%}'";
				else 	$query.= " WHERE dic_s_translation_it like '{%}'";
				break;
			case 4:
				$query= "
									SELECT dic_s_word,dic_s_translation_es as motTraduit $strWordBase
									FROM dictionnary
													";
				if ($word!='' && $word!=null) $query.=" WHERE dic_s_translation_es like '{%".$word."%}'";
				else 	$query.= " WHERE dic_s_translation_es like '{%}'";
				break;
		}
		$query.= " order by dic_s_word asc limit 20";
		//echo "<br>query=".$query;
		$data = $bdd->fwBddGetData($query);
		return $data;
	}

	function getNumberNonTraduit($lang){
		global $bdd;
		switch ($lang){
			case 1:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_fr like '{%}'";
				break;
			case 2:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_uk like '{%}'";
				break;
			case 3:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_it like '{%}'";
				break;
			case 4:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_es like '{%}'";
				break;
		}
		//echo "<br>query=".$query;
		$data = $bdd->fwBddGetData($query);
		if (!empty($data)) return $data[0]['nbr'];
		return 0;
	}
	function getNumberWordDic(){
		global $bdd;
		$query = "select count(*) as nbr from dictionnary";
		$data = $bdd->fwBddGetData($query);
		if (!empty($data)) return $data[0]['nbr'];
		return 0;
	}
	function getAllWordDic($typeExp,$strExp){
		global $bdd;
		if ($typeExp==1) {
			$query = "
								select * from dictionnary
								where dic_s_word like '".$strExp."%'
								order by dic_s_word asc";
		}
		else{
			$query = "
								select * from dictionnary
								where dic_d_maj >= '$strExp'
								order by dic_s_word asc";
		}

		$data = $bdd->fwBddGetData($query);
		return $data;
	}
	function updateDicNonTranslate($word,$word_fr,$word_uk,$word_it,$word_es){
		global $bdd;
		$query= " select * from dictionnary
							where dic_s_word = '".$word."'";
		$data = $bdd->fwBddGetData($query);
		if (!empty($data)){
			$wordBD_fr = $data[0]['dic_s_translation_fr'];
			$wordBD_uk = $data[0]['dic_s_translation_uk'];
			$wordBD_it = $data[0]['dic_s_translation_it'];
			$wordBD_es = $data[0]['dic_s_translation_es'];
			if (preg_match("/\{[a-zA-Z0-9_]\}/", $wordBD_fr)){
				if (!preg_match("/\{[a-zA-Z0-9_]\}/", $word_fr)){
					$word_fr = str_replace("'","''",$word_fr);
					$this->updateWord($word,$word_fr,1);
				}
			}
			else {
				if (strcasecmp($wordBD_fr,$word_fr) <> 0 ){
					$word_fr = str_replace("'","''",$word_fr);
					$this->updateWord($word,$word_fr,1);
				}
			}

			if (preg_match("/\{[a-zA-Z0-9_]\}/", $wordBD_uk)){
				if (!preg_match("/\{[a-zA-Z0-9_]\}/", $word_uk)){
					$word_uk = str_replace("'","''",$word_uk);
					$this->updateWord($word,$word_uk,2);
				}
			}
			else {
				if (strcasecmp($wordBD_uk,$word_uk) <> 0 ){
					$word_uk = str_replace("'","''",$word_uk);
					$this->updateWord($word,$word_uk,2);
				}
			}

			if (preg_match("/\{[a-zA-Z0-9_]\}/", $wordBD_it)){
				if (!preg_match("/\{[a-zA-Z0-9_]\}/", $word_it)){
					$word_it = str_replace("'","''",$word_it);
					$this->updateWord($word,$word_it,3);
				}
			}
			else {
				if (strcasecmp($wordBD_it,$word_it) <> 0 ){
					$word_it = str_replace("'","''",$word_it);
					$this->updateWord($word,$word_it,3);
				}
			}

			if (preg_match("/\{[a-zA-Z0-9_]\}/", $wordBD_es)){
				if (!preg_match("/\{[a-zA-Z0-9_]\}/", $word_es)){
					$word_es = str_replace("'","''",$word_es);
					$this->updateWord($word,$word_es,4);
				}
			}
			else {
				if (strcasecmp($wordBD_es,$word_es) <> 0 ){
					$word_es = str_replace("'","''",$word_es);
					$this->updateWord($word,$word_es,4);
				}
			}
		}
		else{	// ins�rer
			$this->insertNewWord($word,$word_fr,$word_uk,$word_it,$word_es);
		}
	}
	function updateAllDicTranslate($word,$word_fr,$word_uk,$word_it,$word_es){
		global $bdd;
		$this->updateAllWords($word,$word_fr,$word_uk,$word_it,$word_es);
	}
	function delWordDic($motCle){
		global $bdd;
		$query = "delete from dictionnary where dic_s_word='".$motCle."'";
		$bdd->fwBddPostData($query);
		//echo "<br>query=".$query;
	}
	function getInfoWordDic($lang,$motCle){
		global $bdd;
		switch ($lang){
			case 1:
				$query= "
									SELECT dic_s_word,dic_s_translation_fr,dic_s_translation_uk,dic_s_translation_it,dic_s_translation_es
									FROM dictionnary
									WHERE dic_s_translation_fr like '%".$motCle."%'
									order by dic_s_word asc limit 20
									";
				break;
			case 2:
				$query= "
									SELECT dic_s_word,dic_s_translation_fr,dic_s_translation_uk,dic_s_translation_it,dic_s_translation_es
									FROM dictionnary
									WHERE dic_s_translation_uk like '%".$motCle."%'
									order by dic_s_word asc limit 20
									";
				break;
			case 3:
				$query= "
									SELECT dic_s_word,dic_s_translation_fr,dic_s_translation_uk,dic_s_translation_it,dic_s_translation_es
									FROM dictionnary
									WHERE dic_s_translation_it like '%".$motCle."%'
									order by dic_s_word asc limit 20
									";
				break;
			case 4:
				$query= "
									SELECT dic_s_word,dic_s_translation_fr,dic_s_translation_uk,dic_s_translation_it,dic_s_translation_es
									FROM dictionnary
									WHERE dic_s_translation_es like '%".$motCle."%'
									order by dic_s_word asc limit 20
									";

				break;
		}
		$data = $bdd->fwBddGetData($query);

		if (!empty($data)) return $data;
		return 0;
	}
	function getNbrInfoWordDic($lang,$motCle){
		global $bdd;
		switch ($lang){
			case 1:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_fr like '%".$motCle."%'
									";
				break;
			case 2:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_uk like '%".$motCle."%'
									";
				break;
			case 3:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_it like '%".$motCle."%'
									";
				break;
			case 4:
				$query= "
									SELECT count(*) as nbr
									FROM dictionnary
									WHERE dic_s_translation_es like '%".$motCle."%'
									";

				break;
		}
		$data = $bdd->fwBddGetData($query);

		if (!empty($data)) return $data[0]['nbr'];
		return 0;
	}
}